//
// Created by 32827 on 2024/5/19.
//
//
// Created by 32827 on 2024/2/1.
//

#include "user_ui_pp.hpp"
#include "view/user_view_control.hpp"
#if USE_UI_PORT
#include "yzl_ui_include_pp.hpp"
#include "view/yzl_ui_view_pp_1306_demo.hpp"
#include "view/user_view_main.hpp"
#endif


extern YZL_UI_ROOT YZL_UI_Root;
extern YZL_ACTIVE_DATA yzlActiveData; //Copy Form user_ui_main


void * operator new(size_t size)
{
    return YZL_MEM_MANAGER.pMalloc(size);
}

void operator delete (void * pointer)
{
    return YZL_MEM_MANAGER.pFree(pointer);
}
void user_pp_ui_InitView(){
    if(YZL_UI_Root.ViewTop>0){
        yzlActiveData.handleView=&YZL_PreTopView(YZL_UI_Root);
        yzlActiveData.Event=YZL_UI_CTR_EVENT_NONE;
        yzlActiveData.Data=NULL_PTR;
        YZL_PreTopView(YZL_UI_Root).sleep(&yzlActiveData);
    }
    yzlActiveData.handleView=&YZL_TopView(YZL_UI_Root);
    yzlActiveData.Event=YZL_UI_CTR_EVENT_NONE;
    yzlActiveData.Data=NULL_PTR;

    YZL_TopView(YZL_UI_Root).init(NULL_PTR, &yzlActiveData);
    YZL_TopView(YZL_UI_Root).wakeup(&yzlActiveData);
}
void user_pp_ui_delTopView(){
    yzlActiveData.handleView=&YZL_TopView(YZL_UI_Root);
    yzlActiveData.Event=YZL_UI_CTR_EVENT_NONE;
    yzlActiveData.Data=NULL_PTR;

    YZL_TopView(YZL_UI_Root).sleep(&yzlActiveData);
    YZL_TopView(YZL_UI_Root).deInit(&yzlActiveData);

    YZL_UI_Root.ViewTop--;

    yzlActiveData.handleView=&YZL_TopView(YZL_UI_Root);
    YZL_TopView(YZL_UI_Root).wakeup(&yzlActiveData);
}
WidgetButton *widgetButton1;
UserMainView *mainView;
ViewControl *viewControl;

void add_control_listener(void *widget, YZL_UI_CTR_EVENT event){
    if(event==YZL_UI_CTR_EVENT_TOUCH_UP){
        YZL_UI_Root.ViewTop++;
        viewControl=new ViewControl(&YZL_TopView(YZL_UI_Root));
        user_pp_ui_InitView();
    }
}
YZF_RESULT SetText(){
    yz_info("设置中\n");
    mainView->mTime->setText((utf8 *)"00:00:01");
    return YZF_OK;
}
void  user_pp_ui_init(void){
//    widgetButton1=new WidgetButton(&YZL_TopView(YZL_UI_Root).pWidget[YZL_TopView(YZL_UI_Root).WidgetCount], 100, 400, 1, 80, 40,
//                                                 (utf8 *) "Button1");
//    yzlActiveData.handleView=&YZL_TopView(YZL_UI_Root);
//    yzlActiveData.Event=YZL_UI_CTR_EVENT_VIEW_ADD_CHILD;
//    yzlActiveData.Data=YZF_NULL_PTR;
//    YZL_TopView(YZL_UI_Root).act(&yzlActiveData);


    YZL_UI_Root.ViewTop++;
    mainView=new UserMainView(&YZL_TopView(YZL_UI_Root));
    yz_recv_transmit_handler_set(0x00000002,SetText);
    user_pp_ui_InitView();
}
void user_pp_ui_loop(YZL_TOUCH_DATA *data){
    mainView->updateTime();
}